using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PowerManager
{
	public enum PowerAction
	{
		Halt,
		Reboot,
		Logoff,
		Suspend,
		Hibernate
	}

	public partial class PManagerForm : Form
	{
		private PowerSlave slave;

		public PManagerForm(PowerSlave slave)
		{
			InitializeComponent();			
			this.slave = slave;
			fTimer1.TimerElapsed += new FTimerElapsedEventHandler(fTimer1_TimerElapsed);
		}

		void fTimer1_TimerElapsed(object sender, EventArgs e)
		{
			MessageBox.Show("Time finished");
		}
		
		private void useTimerBox_CheckedChanged(object sender, EventArgs e)
		{
			fTimer1.Enabled = useTimerBox.Checked;
		}

		private void sendMessageBox_CheckedChanged(object sender, EventArgs e)
		{
			userMessageBox.Enabled = sendMessageBox.Checked;
		}

		private void okBtn_Click(object sender, EventArgs e)
		{
			if (useTimerBox.Checked)
			{
				fTimer1.Start();
				cancelBtn.Enabled = true;
				actionBox.Enabled = fTimer1.Enabled = userMessageBox.Enabled = okBtn.Enabled = false;
				switch (actionBox.SelectedIndex)
				{
					case 0: // Shutdown
						slave.PerformAction(PowerAction.Halt, fTimer1.Timer.TotalSeconds);
						break;
					case 1: // Reboot
						slave.PerformAction(PowerAction.Reboot, fTimer1.Timer.TotalSeconds);
						break;
					case 3: // Hibernate
						slave.PerformAction(PowerAction.Hibernate,  fTimer1.Timer.TotalSeconds);
						break;
					case 2: // Suspend
						slave.PerformAction(PowerAction.Suspend,  fTimer1.Timer.TotalSeconds);
						break;
					case 4: // Logoff
						slave.PerformAction(PowerAction.Logoff,  fTimer1.Timer.TotalSeconds);
						break;
				}
			}
			else
			{
				switch (actionBox.SelectedIndex)
				{
					case 0: // Shutdown
						slave.PerformAction(PowerAction.Halt);
						break;
					case 1: // Reboot
						slave.PerformAction(PowerAction.Reboot);
						break;
					case 3: // Hibernate
						slave.PerformAction(PowerAction.Hibernate);
						break;
					case 2: // Suspend
						slave.PerformAction(PowerAction.Suspend);
						break;
					case 4: // Logoff
						slave.PerformAction(PowerAction.Logoff);
						break;
				}
			}
		}

		private void cancelBtn_Click(object sender, EventArgs e)
		{
			slave.Cancel();
			fTimer1.Stop();
			cancelBtn.Enabled = false;
			actionBox.Enabled = fTimer1.Enabled = userMessageBox.Enabled = okBtn.Enabled = true;
		}		
	}
}